package com.qf.service.impl;

import com.alibaba.excel.EasyExcel;
import com.qf.mapper.GoodsClassMapper;
import com.qf.pojo.GoodsClass;
import com.qf.service.GoodsClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class GoodsClassServiceImpl implements GoodsClassService {

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    @Override
    public List<GoodsClass> findAllGoodsClass() {
        return goodsClassMapper.selectByExample(null);
    }

    @Override
    public void deleteAllGoodsClass(Integer[] classIds) {
        goodsClassMapper.deleteAllGoodsClass(classIds);
    }

    @Override
    public Integer deleteGoodsClass(Integer classId) {
        return goodsClassMapper.deleteByPrimaryKey(classId);
    }

    @Override
    public void addGoodsClass(GoodsClass goodsClass) {
        goodsClassMapper.insertSelective(goodsClass);
    }

    @Override
    public void updateGoodsClass(GoodsClass goodsClass) {
        goodsClassMapper.updateByPrimaryKeySelective(goodsClass);
    }

    @Override
    public List<GoodsClass> excelGoodsClass(Integer[] classIds) {

        // 1. 获取被打印的数据
        List<GoodsClass> goodsClassList = goodsClassMapper.findGoodsClassByIds(classIds);

        // 2. 设置文件路径
        String fileName = "D:/excel/" + "贵美" + System.currentTimeMillis() + ".xlsx";

        // 3. 判断文件父目录是否存在
        File file = new File(fileName);

        File parentFile = file.getParentFile();

        if (!parentFile.exists()) {

            parentFile.mkdirs();
        }

        // 4. 写入文件
        EasyExcel.write(file, GoodsClass.class).sheet("模板").doWrite(goodsClassList);

        return goodsClassList;
    }

}
